<?php
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------

namespace app\api\logic;

use app\common\logic\LogicBase;
use \tpfcore\Core;
use \think\Db;

class CustomForm extends ApiBase
{
    public function getCustomForm($data=[])
    {
		$result = Core::loadModel($this->name)->getList($data);

		if(empty($result)){

			return [40048,"没有对应的表单"];
			
		}

		$data_['banner']=$result[0]['banner'];
		$data_['form_id']=$result[0]['id'];

		$fields = $result[0]['fields'] ;

		foreach ($fields as $key => $value) {

			$type=$value['type'];

			if($type=="select_type"){

				$select_config = explode("\r\n", $value['exts']['select_config']);

				foreach ($select_config as $k => $v) {

					$arr = explode("|", $v);

					$select_arr[$k]['value']=$arr[1];

					$select_arr[$k]['name']=$arr[0];

					$fields[$key]['exts']['select_config']=$select_arr;

				}

			}
		}
		$data_['data']=$fields;
		return [0,"success",$data_];
    }

    public function saveCustomForm($data)
    {
    	// return [0,"success"];
    	if(empty($data['form_id'])){
    		return [40048,"非法的提交"];
    	}

    	$result = Core::loadModel($this->name)->getList(["where"=>["id"=>$data['form_id']]]);

		if (empty($result)) {
			return [40048, "没有对应的表单"];
		}

		$fields = $result[0]['fields'];

		foreach ($fields as $key => $value) {

			$data_key_name = $value['name'];

			// 数据验证
			$validate = $value['validate'];
			if(!empty($validate) && !preg_match("$validate", $data[$data_key_name], $matches)){
				if (empty($data[$data_key_name])) {
					return [40045,$value['title'] . "不能为空"];
				} else {
					return [40045,$value['title'] . "数据不匹配"];
				}
			}
			$data[$data_key_name]=empty($data[$data_key_name])?$value['exts']['default']:$data[$data_key_name];
		}

		$data['datetime']=time();
		foreach ($data as $key => $value) {
			if(is_array($value)){
				$data[$key]=implode(",", $value);
			}
		}

		$table = $result[0]['table'];

		// $allow_fields= Db::name($table)->getTableInfo('', 'fields');

		$insert = Db::name($table)->strict(false)->field(true)->insert($data);

		if($result){
			return [0,"success"];
		}else{
			return [40046,"添加失败"];
		}
    }

    public function saveRegCustomForm($_data)
    {
    	$token = request()->header("Auth-Token");
    	if(empty($token)){
    		return [40048, '还未登录~'];
    	}

    	if(empty($_data['form_id'])){
    		return [40048, '非法的提交'];
    	}

    	$CustomForm = Core::loadModel($this->name)->getOneObject(['id'=>$_data['form_id']]);
		if (empty($CustomForm)) {
			return [40048, '没有对应的表单'];
		}

		Db::startTrans();

    	$User = Core::loadModel('User')->where(['token'=>$token, 'type'=>0])->find();
    	if (empty($User)) {
    		Db::rollback();
    		return [40048, '无此用户~'];
    	}
    	if (1 === $User->isresult) {
    		Db::rollback();
			return [0, 'success'];
    	}

    	foreach ($CustomForm->fields as $key => $value) {

			$data_key_name = $value['name'];

			$validate = $value['validate'];
			if (!empty($validate) && !preg_match("$validate", $_data[$data_key_name], $matches)) {
				Db::rollback();
				if (empty($_data[$data_key_name])) {
					return [40045, $value['title'] . "不能为空"];
				} else {
					return [40045, $value['title'] . "数据不匹配"];
				}
			}
			$_data[$data_key_name] = empty($_data[$data_key_name]) ? $value['exts']['default'] : $_data[$data_key_name];
		}

		$_data['uid'] = $User->id;
		$_data['openid'] = $User->openid;
		$_data['status'] = 1;

		$User->isresult = 1;
		if (false === $User->save()) {
			Db::rollback();
			return [40003, '网络繁忙，请稍后再试！'];
		}

		$_data['datetime'] = time();
		foreach ($_data as $key => $value) {
			if (is_array($value)) {
				$_data[$key]=implode(',', $value);
			}
		}

		$table = $CustomForm->getData('table');

		$result = Db::name($table)->strict(false)->field(true)->insert($_data);
		if (!$result) {
			Db::rollback();
			return [40046, '网络繁忙，请稍后再试'];
		}

		Db::commit();

		return [0, 'success'];
    }
}